package cn.zhaosunny.soap

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 *
 * @author zhaoyang 2021/12/1
 */
class SoapLogInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        logRequest(request)
        val response: Response = chain.proceed(request)
        return logResponse(response)
    }

    private fun logRequest(request: Request) {
        try {
            val url = request.url.toString()
            val headers = request.headers
            val requestBody = request.body
            System.err.println(url)
            System.err.println(headers)
            System.err.println(requestBody)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun logResponse(response: Response): Response {
        try {
            val builder: Response.Builder = response.newBuilder()
            val clone: Response = builder.build()
            System.err.println(clone.body)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return response
    }

}